// Word2Txt.cpp: implementation of the CWord2Txt class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TestTTS.h"
#include "Word2Txt.h"

#include <atlbase.h>
#include "msword9.h"//also need AfxOleInit() in InitInstance()

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWord2Txt::CWord2Txt()
{

}

CWord2Txt::~CWord2Txt()
{

}

bool CWord2Txt::Save2Txt(const char* szWordFileName, const char* szTxtFileName)
{
	if(strlen(szWordFileName) == 0)
		return false;

	_Application app;
	COleException err;
	app.CreateDispatch(_T("Word.Application"), &err);
	if(app.m_lpDispatch == NULL)
	{
		AfxMessageBox("Sorry, MS Word is not installed.");
		return false;
	}
	
	app.SetDisplayAlerts(FALSE);
//	app.SetVisible(TRUE);
	
	Documents docs = app.GetDocuments();
/*	 ActiveDocument.SaveAs FileName:="12th test.txt", FileFormat:=wdFormatText, _
         LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False
*/
	CComVariant vtName(szWordFileName), vtTrue(true), vtFalse(false), vtEmpty(""), vtZero(0);
	docs.Open(&vtName, &vtFalse, &vtTrue, &vtFalse, &vtEmpty, &vtEmpty, &vtFalse, &vtEmpty, &vtEmpty, &vtZero, &vtFalse, &vtTrue);
	
	_Document doc = app.GetActiveDocument();
	CComVariant vtSaveName(szTxtFileName), vtFormatText(2);
	doc.SaveAs(&vtSaveName, &vtFormatText, &vtFalse, &vtEmpty, &vtTrue, &vtEmpty, &vtFalse,
		&vtFalse, &vtFalse, &vtFalse, &vtFalse);

	CComVariant a,b;
	app.Quit(&vtFalse, &a, &b);
	app.ReleaseDispatch();

	return true;
}